package com.invengo.module_check.ui.fragment.get_in_car;

import static me.goldze.mvvmhabit.utils.constant.Constants.TOKEN_CAMERA;

import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.zxing.integration.android.IntentIntegrator;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.databinding.FragmentGetInCarBinding;

import androidx.annotation.Nullable;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class GetInCarFragment extends BaseFragment<FragmentGetInCarBinding, GetInCarViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_get_in_car;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public String getRightTitle() {
        return "扫书车";
    }

    @Override
    public void onRightEvent(View view) {
        if (cameraCount > 0)
            openCamera();
        else
            ToastUtils.showShort("该设备无相机，无法使用扫描功能！！");
//        Messenger.getDefault().send("ABC1234567",TOKEN_CAMERA);
    }

    private int cameraCount = 0;

    @Override
    public void initData() {
        cameraCount = Camera.getNumberOfCameras();
//        openCamera();
//        new IntentIntegrator(getActivity()).initiateScan();
//        IntentIntegrator integrator = IntentIntegrator.forSupportFragment(this);
    }

    private MaterialDialog dirtyDialog, errorDialog, bind2CarDialog;

    @Override
    public void initViewObservable() {

        viewModel.ui.showDirtyDialogEvent.observe(this, integer -> {
            if (dirtyDialog == null) {
                dirtyDialog = MaterialDialogUtils.showBasicDialog(getActivity(), "改书车上存在" + integer + "册遗留的图书，是否删除？")
                        .title("温馨提示！")
                        .contentGravity(GravityEnum.CENTER)
                        .negativeText("取消")
                        .onNegative((dialog, which) -> {
                            dialog.dismiss();
                        })
                        .positiveText("确定")
                        .onPositive((dialog, which) -> {
                            dialog.dismiss();
                            viewModel.removeDirtyItem();
                        })
                        .build();
            }
            if (!dirtyDialog.isShowing())
                dirtyDialog.show();
        });

        viewModel.ui.showBind2CarDialog.observe(this, content -> {
            if (bind2CarDialog == null) {
                bind2CarDialog = MaterialDialogUtils.showBasicDialog(getActivity(), "温馨提示！")
                        .content(content)
                        .contentGravity(GravityEnum.CENTER)
                        .positiveText("确定")
                        .onPositive((dialog, which) -> {
                            dialog.dismiss();
                            viewModel.bind2Car();
                        }).build();
            }
            if (!bind2CarDialog.isShowing())
                bind2CarDialog.show();
        });
    }

    private void openCamera() {
        IntentIntegrator integrator = new IntentIntegrator(getActivity());
        integrator.setOrientationLocked(true);
        integrator.initiateScan();
    }
}
